<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item :label="t('candidate.fields.processInstanceId')" prop="processInstanceId">
        <el-input
          v-model="formData.processInstanceId"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.processInstanceId')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.accountId')" prop="accountId">
        <el-input
          v-model="formData.accountId"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.accountId')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.awardId')" prop="awardId">
        <el-input
          v-model="formData.awardId"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.awardId')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.studentLevel')" prop="studentLevel">
        <el-input
          v-model="formData.studentLevel"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.studentLevel')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.templateId')" prop="templateId">
        <el-input
          v-model="formData.templateId"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.templateId')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.awardLevel')" prop="awardLevel">
        <el-input
          v-model="formData.awardLevel"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.awardLevel')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.jobCode')" prop="jobCode">
        <el-input
          v-model="formData.jobCode"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.jobCode')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.major')" prop="major">
        <el-input
          v-model="formData.major"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.major')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.studentName')" prop="studentName">
        <el-input
          v-model="formData.studentName"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.studentName')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.awardMajor')" prop="awardMajor">
        <el-input
          v-model="formData.awardMajor"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.awardMajor')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.gender')" prop="gender">
        <el-input
          v-model="formData.gender"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.gender')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.accountType')" prop="accountType">
        <el-select
          v-model="formData.accountType"
          :placeholder="t('common.selectTextPlaceholder') + t('candidate.fields.accountType')"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('candidate.fields.mentorId')" prop="mentorId">
        <el-input
          v-model="formData.mentorId"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.mentorId')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.mentorName')" prop="mentorName">
        <el-input
          v-model="formData.mentorName"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.mentorName')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.mentorCode')" prop="mentorCode">
        <el-input
          v-model="formData.mentorCode"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.mentorCode')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.researchDeptId')" prop="researchDeptId">
        <el-input
          v-model="formData.researchDeptId"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.researchDeptId')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.researchDept')" prop="researchDept">
        <el-input
          v-model="formData.researchDept"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.researchDept')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.processStatus')" prop="processStatus">
        <el-radio-group v-model="formData.processStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('candidate.fields.qualityStatus')" prop="qualityStatus">
        <el-radio-group v-model="formData.qualityStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('candidate.fields.applySource')" prop="applySource">
        <el-input
          v-model="formData.applySource"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.applySource')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.attachment')" prop="attachment">
        <el-input
          v-model="formData.attachment"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.attachment')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.historyAwardsJson')" prop="historyAwardsJson">
        <el-input
          v-model="formData.historyAwardsJson"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.historyAwardsJson')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.selfIntroduction')" prop="selfIntroduction">
        <el-input
          v-model="formData.selfIntroduction"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.selfIntroduction')"
        />
      </el-form-item>
      <el-form-item :label="t('candidate.fields.remark')" prop="remark">
        <el-input
          v-model="formData.remark"
          :placeholder="t('common.inputTextPlaceholder') + t('candidate.fields.remark')"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">{{
        t('common.ok')
      }}</el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { CandidateApi, CandidateVO } from '@/api/awards/candidate'
import { FormRules } from 'element-plus'

/** 候选人 表单 */
defineOptions({ name: 'CandidateForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  processInstanceId: undefined,
  accountId: undefined,
  awardId: undefined,
  studentLevel: undefined,
  templateId: undefined,
  awardLevel: undefined,
  jobCode: undefined,
  major: undefined,
  studentName: undefined,
  awardMajor: undefined,
  gender: undefined,
  accountType: undefined,
  mentorId: undefined,
  mentorName: undefined,
  mentorCode: undefined,
  researchDeptId: undefined,
  researchDept: undefined,
  processStatus: undefined,
  qualityStatus: undefined,
  applySource: undefined,
  attachment: undefined,
  historyAwardsJson: undefined,
  selfIntroduction: undefined,
  remark: undefined
})
const formRules = reactive<FormRules>({
  id: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.id'),
      trigger: 'blur'
    }
  ],
  processInstanceId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.processInstanceId'),
      trigger: 'blur'
    }
  ],
  accountId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.accountId'),
      trigger: 'blur'
    }
  ],
  awardId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.awardId'),
      trigger: 'blur'
    }
  ],
  studentLevel: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.studentLevel'),
      trigger: 'blur'
    }
  ],
  templateId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.templateId'),
      trigger: 'blur'
    }
  ],
  awardLevel: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.awardLevel'),
      trigger: 'blur'
    }
  ],
  jobCode: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.jobCode'),
      trigger: 'blur'
    }
  ],
  major: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.major'),
      trigger: 'blur'
    }
  ],
  studentName: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.studentName'),
      trigger: 'blur'
    }
  ],
  awardMajor: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.awardMajor'),
      trigger: 'blur'
    }
  ],
  gender: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.gender'),
      trigger: 'blur'
    }
  ],
  accountType: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.accountType'),
      trigger: 'change'
    }
  ],
  mentorId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.mentorId'),
      trigger: 'blur'
    }
  ],
  mentorName: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.mentorName'),
      trigger: 'blur'
    }
  ],
  mentorCode: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.mentorCode'),
      trigger: 'blur'
    }
  ],
  researchDeptId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.researchDeptId'),
      trigger: 'blur'
    }
  ],
  researchDept: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.researchDept'),
      trigger: 'blur'
    }
  ],
  processStatus: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.processStatus'),
      trigger: 'blur'
    }
  ],
  qualityStatus: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.qualityStatus'),
      trigger: 'blur'
    }
  ],
  applySource: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.applySource'),
      trigger: 'blur'
    }
  ],
  attachment: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.attachment'),
      trigger: 'blur'
    }
  ],
  historyAwardsJson: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.historyAwardsJson'),
      trigger: 'blur'
    }
  ],
  selfIntroduction: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.selfIntroduction'),
      trigger: 'blur'
    }
  ],
  remark: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.remark'),
      trigger: 'blur'
    }
  ],
  creator: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.creator'),
      trigger: 'blur'
    }
  ],
  createTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.createTime'),
      trigger: 'blur'
    }
  ],
  updater: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.updater'),
      trigger: 'blur'
    }
  ],
  updateTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.updateTime'),
      trigger: 'blur'
    }
  ],
  deleted: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.deleted'),
      trigger: 'blur'
    }
  ],
  tenantId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('candidate.fields.tenantId'),
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CandidateApi.getCandidate(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CandidateVO
    if (formType.value === 'create') {
      await CandidateApi.createCandidate(data)
      message.success(t('common.createSuccess'))
    } else {
      await CandidateApi.updateCandidate(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    processInstanceId: undefined,
    accountId: undefined,
    awardId: undefined,
    studentLevel: undefined,
    templateId: undefined,
    awardLevel: undefined,
    jobCode: undefined,
    major: undefined,
    studentName: undefined,
    awardMajor: undefined,
    gender: undefined,
    accountType: undefined,
    mentorId: undefined,
    mentorName: undefined,
    mentorCode: undefined,
    researchDeptId: undefined,
    researchDept: undefined,
    processStatus: undefined,
    qualityStatus: undefined,
    applySource: undefined,
    attachment: undefined,
    historyAwardsJson: undefined,
    selfIntroduction: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}
</script>
